package com.cgy.utils.net.http.stream;

import com.cgy.utils.net.http.config.RequestConfig;
import com.cgy.utils.net.http.enums.HttpMethod;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 流式
 * @author CHTK
 */
public class OkHttpStreamBuilder {

    private final OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();

    private Request.Builder builder;
    private HttpMethod httpMethod;
    private RequestConfig requestConfig;

    public OkHttpStreamBuilder(HttpMethod method, RequestConfig requestConfig, Request.Builder builder) {
        this.builder = builder;
        this.requestConfig = requestConfig;
        this.httpMethod = method;
    }

    /**
     *
     * @return
     */
    public Response execute() {

        OkHttpClient okHttp = getRequest();
        RequestBody requestBody = requestConfig.getRequestBody();

        if(httpMethod == HttpMethod.GET) {
            try {
                return okHttp.newCall(this.builder.build()).execute();
            } catch (IOException e) {
                if(null != requestConfig.getThrowable()) {
                    return requestConfig.getThrowable().throwable(e);
                }
            }
        } else if(httpMethod == HttpMethod.POST) {
            Request.Builder post = null;
            if(null == requestBody) {
                FormBody formBody = requestConfig.getFormBody().build();
                post = this.builder.post(formBody);
            } else {
                post = this.builder.post(requestBody);
            }
            try {
                return okHttp.newCall(post.build()).execute();
            } catch (IOException e) {
                if(null != requestConfig.getThrowable()) {
                    return requestConfig.getThrowable().throwable(e);
                }
            }
        } else if(httpMethod == HttpMethod.DELETE) {
            Request.Builder post = null;
            if(null == requestBody) {
                FormBody formBody = requestConfig.getFormBody().build();
                post = this.builder.delete(formBody);
            } else {
                post = this.builder.delete(requestBody);
            }
            try {
                return okHttp.newCall(post.build()).execute();
            } catch (IOException e) {
                if(null != requestConfig.getThrowable()) {
                    return requestConfig.getThrowable().throwable(e);
                }
            }
        } else if(httpMethod == HttpMethod.PUT) {
            Request.Builder post = null;
            if(null == requestBody) {
                FormBody formBody = requestConfig.getFormBody().build();
                post = this.builder.put(formBody);
            } else {
                post = this.builder.put(requestBody);
            }
            try {
                return okHttp.newCall(post.build()).execute();
            } catch (IOException e) {
                if(null != requestConfig.getThrowable()) {
                    return requestConfig.getThrowable().throwable(e);
                }
            }
        }
        return null;
    }
    /**
     *
     * @return
     */
    public void execute(final Callback callback) {
        OkHttpClient okHttp = getRequest();
        RequestBody requestBody = requestConfig.getRequestBody();
        Request.Builder post = null;

        if(httpMethod == HttpMethod.GET) {
            okHttp.newCall(this.builder.build()).enqueue(callback);
        } else if(httpMethod == HttpMethod.POST) {
            if(null == requestBody) {
                FormBody formBody = requestConfig.getFormBody().build();
                post = this.builder.post(formBody);
            } else {
                post = this.builder.post(requestBody);
            }
            okHttp.newCall(post.build()).enqueue(callback);
        } else if(httpMethod == HttpMethod.DELETE) {
            if(null == requestBody) {
                FormBody formBody = requestConfig.getFormBody().build();
                post = this.builder.delete(formBody);
            } else {
                post = this.builder.delete(requestBody);
            }
            okHttp.newCall(post.build()).enqueue(callback);
        } else if(httpMethod == HttpMethod.PUT) {
            if(null == requestBody) {
                FormBody formBody = requestConfig.getFormBody().build();
                post = this.builder.put(formBody);
            } else {
                post = this.builder.put(requestBody);
            }
            okHttp.newCall(post.build()).enqueue(callback);
        }
    }

    /**
     *
      * @return
     */
    private OkHttpClient getRequest() {
        if(requestConfig.getReadTimeout() > 0) {
            okHttpClient.readTimeout(requestConfig.getReadTimeout(), TimeUnit.SECONDS);
        }
        if(requestConfig.getConnectTimeout() > 0) {
            okHttpClient.connectTimeout(requestConfig.getConnectTimeout(), TimeUnit.SECONDS);
        }
        if(null != requestConfig.getInterceptor()) {
            okHttpClient.addInterceptor(requestConfig.getInterceptor());
        }
        if(null != requestConfig.getSslSocketFactory()) {
            okHttpClient.sslSocketFactory(requestConfig.getSslSocketFactory());
        }

        OkHttpClient okHttp = okHttpClient.build();
        return okHttp;
    }
}
